﻿using RLAWEB.Models;
using RLAWEB.RLAServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RLAWEB
{
    public partial class ListFriends : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ListFriendsRepeater.DataSource = GetFriends();
                ListFriendsRepeater.DataBind();

                AcceptFriends.DataSource = GetFriendsAsUsers();
                AcceptFriends.DataBind();
            }
        }

        private List<User> GetFriendsAsUsers()
        {
            List<User> result = new List<User>();
            User loggedOnUser = Helper.GetLoggedInUser(Request.Cookies[FormsAuthentication.FormsCookieName]);
            if (loggedOnUser != null)
            {
                IRealLifeService _service = new RealLifeServiceClient();
                List<Friend> friendList = _service.GetPendingFriendships(loggedOnUser);
                foreach (Friend friend in friendList)
                {
                    if (friend.Requested.UserId == loggedOnUser.UserId)
                    {
                        result.Add(friend.Requester);
                    }
                    else
                    {
                        result.Add(friend.Requested);
                    }
                }
            }
            return result;
        }

        private List<User> GetFriends()
        {
            List<User> listFriendUsers = new List<User>();
            var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie != null)
            {
                IRealLifeService _service = new RealLifeServiceClient();
                listFriendUsers = _service.GetFriendships(Helper.GetLoggedInUser(authCookie));
            }
            return listFriendUsers;
        }

        protected void lblPendingFriend_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IRealLifeService _service = new RealLifeServiceClient();
                Label lblPending = (Label)sender;
                int pendingFriend = int.Parse(lblPending.Text);
                User user = _service.GetUserById(pendingFriend);
                List<Friend> pendingFriends = _service.GetPendingFriendships(user);
                User loggedOnUser = Helper.GetLoggedInUser(Request.Cookies[FormsAuthentication.FormsCookieName]);
                if (loggedOnUser != null)
                {
                    bool found = false;
                    int index = 0;

                    while (!found && index < pendingFriends.Count)
                    {
                        Friend friend = pendingFriends[index];
                        if (friend.Requester.UserId == pendingFriend)
                        {
                            if (friend.Requested.UserId == loggedOnUser.UserId)
                            {
                                found = true;
                            }
                            else
                            {
                                index++;
                            }
                        }
                        else
                        {
                            if (friend.Requester.UserId == loggedOnUser.UserId)
                            {
                                found = true;
                            }
                            else
                            {
                                index++;
                            }
                        }
                    }

                    Friend foundFriend = pendingFriends[index];
                    if (foundFriend.Requester.UserId == pendingFriend)
                    {
                        lblPending.Visible = false;
                    }
                    else
                    {
                        lblPending.Visible = true;
                        lblPending.Text = "Afventer svar";
                    }
                }
            }
        }

        protected void btnAcceptFriend_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IRealLifeService _service = new RealLifeServiceClient();
                Button btnAccept = (Button)sender;
                int pendingFriend = int.Parse(btnAccept.Text);
                User user = _service.GetUserById(pendingFriend);
                List<Friend> pendingFriends = _service.GetPendingFriendships(user);
                User loggedOnUser = Helper.GetLoggedInUser(Request.Cookies[FormsAuthentication.FormsCookieName]);
                if (loggedOnUser != null)
                {
                    bool found = false;
                    int index = 0;

                    while (!found && index < pendingFriends.Count)
                    {
                        Friend friend = pendingFriends[index];
                        if (friend.Requester.UserId == pendingFriend)
                        {
                            if (friend.Requested.UserId == loggedOnUser.UserId)
                            {
                                found = true;
                            }
                            else
                            {
                                index++;
                            }
                        }
                        else
                        {
                            if (friend.Requester.UserId == loggedOnUser.UserId)
                            {
                                found = true;
                            }
                            else
                            {
                                index++;
                            }
                        }
                    }

                    Friend foundFriend = pendingFriends[index];
                    if (foundFriend.Requester.UserId == pendingFriend)
                    {
                        btnAccept.Visible = true;
                        btnAccept.Text = "Acceptér ven";
                    }
                    else
                    {
                        btnAccept.Visible = false;
                    }
                }
            }
        }

        protected void btnAcceptFriend_Click(object sender, EventArgs e)
        {
            IRealLifeService _service = new RealLifeServiceClient();
            User loggedOnUser = Helper.GetLoggedInUser(Request.Cookies[FormsAuthentication.FormsCookieName]);
            int friendId = int.Parse(Request.QueryString["ID"]);
            User friend = _service.GetUserById(friendId);
            _service.SetFriendshipStatus(friend, loggedOnUser, Friend.Status.Accepted);
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            Response.Redirect(url);
        }
    }
}